#pragma once
#include "DXAMLCommon.h"

#include "CXcpList.h"


class CType;
class CDependencyObject;
class CDependencyProperty;
class CValue;
class CNameBuilder;
class CFrameworkTemplate;

class NamespaceInfo
{
public: 
	NamespaceInfo(CXString *, XINT32, XINT32, enum eKnownNamespace);
	~NamespaceInfo();
	void SetClrNamespace(XUINT32, XUINT16 *, XUINT32, XUINT16 *);
	void TrySetIgnored();
	XINT32 IsIgnored();
	XINT32 IsDefault();
	XINT32 IsClrNamespace();
	XINT32 IsUnderstood();
	CXString * GetClrNamespaceName();
	CXString * GetAssemblyName();
	CXString * GetNamespaceUri();
	void UpdateWithRootAssemblyName(XUINT32, XUINT16 *);
	enum eKnownNamespace m_eKnownNamespace;
private: 
	CXString m_strClrNamespace;
	CXString m_strAssemblyName;
	CXString * m_pstrNamespaceUri;
	struct 
	{
		XUINT16 m_fIsIgnored:1;
		XUINT16 m_fIsUnderstood:1;
		XUINT16 m_fIsDefault:1;
		XUINT16 m_fIsClrNamespace:1;
	};


};


struct NamespaceInfoPair
{
	NamespaceInfoPair();
	~NamespaceInfoPair();
	CXString * m_pPrefix;
	NamespaceInfo * m_pNamespaceInfo;
};

class CMarkupExtensionDescription
{
public: 
	enum MarkupExtensionType
	{
		Unknown = 0x0,
		Binding = 0x1,
		StaticResource = 0x2,
		TemplateBinding = 0x3,
		Null = 0x4,
		RelativeSource = 0x5
	};

	CMarkupExtensionDescription(CCoreServices *);
	~CMarkupExtensionDescription();
	HRESULT CreateBindingExtension();
	HRESULT SetAttribute();
	enum MarkupExtensionType m_iType;
	CXString m_strPositionalArgument;
	CXString m_strPropertyName;
	CXString m_strPropertyValue;
	CDependencyObject * m_pExtension;
	CCoreServices * m_pCore;
};


class CXAMLNode
{
public: 
	CXAMLNode();
	HRESULT EnsurePropertyBits();
	HRESULT TransferPropertyBits(CXAMLNode *);
	HRESULT IsPropertySet(CDependencyProperty *, XUINT32 *);
	HRESULT MarkPropertySet(CDependencyProperty *);
	XUINT32 IsPropertyElement();
	HRESULT SetValueOnThisNodesDO(CDependencyProperty *, CValue *);
	~CXAMLNode();
	CXAMLNode * m_pNext;
	XUINT32 m_cPrefix;
	XUINT16 * m_pPrefix;
	XUINT32 m_cName;
	XUINT16 * m_pName;
	CDependencyObject * m_pDependencyObject;
	CDependencyProperty * m_pdp;
	CXString * m_pKey;
	XUINT32 * m_pPropertySetBits;
	XUINT32 m_cPropertySetBits;
	XUINT32 m_uiSavedLine;
	XUINT32 m_uiSavedColumn;
	XUINT32 m_uiCustomTypeId;
	XUINT16 * m_pNamespaceUri;
	NamespaceInfo * m_pNamespace;
	CValueStore * m_pDeclaredNamespaces;
	XUINT32 m_iNameDotPosition;
	XUINT32 m_uiTypeFlags;

	struct
	{
		XUINT32 m_fIsManagedProperty:1;
		XUINT32 m_fIsManagedElement:1;
		XUINT32 m_fIsUnknownProperty:1;
		XUINT32 m_fPreContentProperty:1;
		XUINT32 m_fHaveCheckedPropertyElementType:1;
		XUINT32 m_fCreationDeferred:1;
	};


};

class CSL3Parser
{
private: 
	static HRESULT __stdcall Create(CCoreServices *, CSL3Parser * *);
	CSL3Parser(CCoreServices *);
	~CSL3Parser();
public: 
	static HRESULT __stdcall LoadXaml(CCoreServices *, XUINT32, XUINT8 *, struct Xcp_IDependencyObject *, struct Xcp_IDependencyObject *, struct Xcp_IDependencyObject * *, XINT32, XUINT32, struct Xcp_IDependencyObject *, XUINT32, struct IValueStore *, XUINT32, XUINT16 *, XUINT32, XUINT32);
	static HRESULT __stdcall LoadXaml(CCoreServices *, XUINT32, XUINT8 *, struct Xcp_IDependencyObject *, struct Xcp_IDependencyObject *, struct Xcp_IDependencyObject * *, XINT32, XUINT32, struct Xcp_IDependencyObject *, XUINT32, XUINT32, XUINT16 *, XUINT32, XUINT32);
	static XUINT32 __stdcall IsValidXamlName(XUINT32, XUINT16 *);
	static XUINT32 __stdcall IsValidJavascriptName(XUINT32, XUINT16 *);
	HRESULT LookupNamespacePrefix(XUINT32, XUINT16 *, NamespaceInfo * *);
	HRESULT ResolveElementType(XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT32, struct Xcp_IDependencyObject *);
	HRESULT GetTargetType(CType * *);
	struct Xcp_IDependencyObject * GetRootObject();
	HRESULT ParseStaticResource(XUINT32, XUINT16 *, CDependencyObject * *);
	HRESULT AddNamespace(XUINT16 *, XUINT32, XINT32, XINT32, enum eKnownNamespace, XUINT32);
private: 
	HRESULT setError(XUINT32, XUINT32, XUINT32, XUINT32, struct XSTRING * *);
	XUINT32 isCLRError(HRESULT);
	HRESULT setErrorFromCLR(HRESULT, XUINT32, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32);
	static HRESULT __stdcall ValidateDefaultNamespace(XUINT32, XUINT16 *);
	static HRESULT __stdcall PreprocessFirstElement(struct IPALReader *, CSL3Parser *, XUINT32 *, XUINT16 * *, XUINT32 *, XUINT32 *);
	static HRESULT __stdcall PopulateIgnorableNamespaces(XUINT16 *, XUINT32, CValueStore *);
	HRESULT PreprocessElementAttributes(struct IPALReader *);
	HRESULT GetNamespaceInfo(XUINT32, const XUINT16 *, XUINT32, const XUINT16 *, NamespaceInfo * *);
	HRESULT StartDocument();
	HRESULT EndDocument();
	HRESULT ProcessStartElement(XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT32);
	HRESULT StartElement(XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT32, struct Xcp_IDependencyObject *);
	HRESULT StartObjectElement(XUINT32, XUINT32, XINT32);
	HRESULT StartPropertyElement(XUINT32, XUINT16 *, XUINT32, XUINT16 *);
	HRESULT EndElement(XUINT32, XUINT32);
	HRESULT SetAttributeWithMarkupExtension(NamespaceInfo *, CMarkupExtensionDescription *, CDependencyProperty *, XUINT32, XUINT16 *, enum AttachedPropertyQueryStatus, XUINT32, XUINT16 *, XUINT32, XUINT32);
	HRESULT ApplyStaticResource(NamespaceInfo *, CXAMLNode *, CDependencyProperty *, CXString *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT32);
	HRESULT Attribute(XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT32, enum AttributeProcessingPhase);
	HRESULT Characters(XUINT32, XUINT16 *);
	HRESULT AccumulateCharacters(XUINT32, XUINT16 *);
	HRESULT AccumulateWhitespace(XUINT32, XUINT16 *);
	HRESULT ClearAccumulatedCharacters();
	HRESULT ValidateParentChild(CXAMLNode *, CDependencyProperty *, XUINT32, XUINT16 *, XUINT32, XUINT32);
	XUINT32 IsResource();
	static HRESULT __stdcall CacheResourceReference(CSL3Parser *, CMarkupExtensionDescription *);
	HRESULT XAMLUnparserNoNeedFullClose();
	HRESULT XAMLUnparserCloseStartElementTag();
	HRESULT XAMLUnparserAppendCloseAngleBracket(XUINT32);
	HRESULT AttemptPerPropertyConversion(const CDependencyProperty *, XUINT32, const XUINT16 *, CDependencyObject * *);
	HRESULT ResolveDependencyPropertyForAttachedProperty(CDependencyProperty * *, XUINT32, XUINT16 *);
	void InitializeQuirksMode();
	static XUINT32 __stdcall HasMarkupExtensionEscaping(XUINT32, const XUINT16 *);
	HRESULT RegisterNamedItem(XUINT32, XUINT16 *, struct Xcp_IDependencyObject *, XUINT32, class CDependencyObject);
	XINT32 m_bXAMLUnparserCreate;
public: 
	XUINT32 IsInUnparserMode();
	HRESULT XAMLUnparserSaveNamespaceDeclaration(XUINT32, const XUINT16 *, XUINT32, const XUINT16 *);
private: 
	struct Xcp_IDependencyObject * m_pDependencyObject;
	CXAMLNode * m_pStack;
	CXAMLNode * m_pRootNode;
	CNameBuilder * m_pName;
	CCoreServices * m_pCore;
	XINT32 m_bValid;
	XINT32 m_bRequireDefaultNamespace;
	XINT32 m_bAllowEventHandlers;
	XUINT32 m_bAllowCoreTypeExistingFrameworkRoot;
	XUINT32 m_fErrorRecorded;
	struct Xcp_IDependencyObject * m_pRoot;
	struct Xcp_IDependencyObject * m_pEventRoot;
	XUINT32 m_bRootIsFrameworkElement;
	XUINT16 * m_pFrameworkElementNamespaceUri;
	struct Xcp_IDependencyObject * m_pExistingFrameworkRoot;
	struct Xcp_IDependencyObject * m_pTemplatedParent;
	XUINT32 m_bRegisterNamesInTemplateNamescope;
	XUINT32 m_cXClass;
	XUINT16 * m_pXClass;
	XUINT32 m_cSourceAssemblyName;
	XUINT16 * m_pSourceAssemblyName;
	CValueStore * m_pNamespaceStore;
	NamespaceInfo * m_pDefaultNamespace;
	CXString * m_pHiddenName;
	CXString * m_pstrAccumulatedText;
	CXString * m_pstrTempClassName;
	HRESULT XAMLUnparserAddAttribute(XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *, XUINT32, XUINT16 *);
	HRESULT XAMLUnparserCheckSize(XUINT32);
	HRESULT XAMLUnparserPrintPrefix(XUINT32, XUINT16 *, XUINT32, XUINT16 *);
	HRESULT XAMLUnparserExtractAndSaveNamespaceDeclaration(XUINT32, XUINT16 *);
	HRESULT XAMLUnparserPrintString(XUINT32, XUINT16 *);
	HRESULT XAMLUnparserPrintStringEscapingEntities(XUINT32, XUINT16 *);
	HRESULT XAMLUnparserPrintNamespaces();
	HRESULT XAMLUnparserDoTemplateWorkaround(CXAMLNode *);
	XUINT32 XAMLUnparserShouldStartTemplateWorkaround(CXAMLNode *, XUINT32, XUINT16 *);
	CXString * m_pXAMLUnparser;
	XINT32 m_bXAMLUnparserFirstElement;
	XINT32 m_nXAMLUnparserDepth;
	XUINT32 m_nXAMLUnparserCountToNamespace;
	CXcpList<NamespaceInfoPair> * m_pXAMLUnparserNamespaces;
	XINT32 m_bXAMLUnparserFullTagClose;
	struct IValueStore * m_pXAMLUnparserResources;
	XUINT32 m_eXMLUnparserMode;
	CFrameworkTemplate * m_pFrameworkTemplate;
	XINT32 m_nXAMLTempUnparserDepth;
	CXString * m_pXAMLTempUnparser;
	XINT32 m_nXAMLUnparserTempCountToNamespace;
	CXString * m_pXAMLUnparserParentName;
	XUINT32 m_bShouldEscapeMarkupExtensions;
	XUINT32 m_bShouldInsertIncorrectSpaceAfterCloseTagsInTemplate;
	XUINT32 m_bShouldAttachedPropertyAttributesInheritPrefix;
	XUINT32 m_bShouldSupportImplicitStyle;
	XUINT32 m_bExpandTemplatesDuringParse;
	XUINT32 m_nXAMLUnparserTemplateDepth;
	struct IValueStore * m_pResourcesForOriginalScope;

};

